package graffeo.osu;

import java.util.ArrayList;
import java.util.Random;

import android.content.Context;
import android.widget.GridView;

public class PlayField {

	public ArrayList<Integer> playField;
	public int width;
	public int height;
	public int robotCount;
	public Robot[] bots;
	public int robotDeathCounter;
	public int teleportCounter;
	public boolean winToken;
	public String temp;
	
	private GridView field;
	
	// UPDATE MOVEMENT TO REFLECT
	public PlayField(int rCount, Context context, GridView field) {
		width = 20;
		height = 20;
		playField = new ArrayList<Integer>();
		for (int row = 0; row < 20; row++) 
			for (int column = 0; column < 20; column++)
				playField.add(0);
				
		this.field = field;
		field.setAdapter(new ListAdapter(context, R.layout.cell, playField));

		robotCount = rCount;
		bots = new Robot[rCount];
		placeRobot();
		robotDeathCounter = 0;
		teleportCounter = 10;
		winToken = false;

	}

	// ROBOT SECTION
	public void placeRobot() {
		Random rand = new Random();
		for (int i = 0; i < robotCount; i++) {
			int xCoord = rand.nextInt(20);
			int yCoord = rand.nextInt(20);
			bots[i] = new Robot(xCoord, yCoord);
			
			playField.set(yCoord * 20 + xCoord, ListAdapter.ROBOT);
		}
		
		field.invalidateViews();
	}

	public ArrayList<Integer> moveRobot(Player x) {
		if (x.getIsDead() == false) {
			int playerXPos = x.getXCoord();
			int playerYPos = x.getYCoord();
			// FOR LOOP TO MOVE EVERY ROBOT IN THE ARRAY
			for (int counter = 0; counter < robotCount; counter++) {
				// IF ROBOT ISNT DEAD
				if (bots[counter].getIsDead() == false) {
					// MOVE LOGIC
					// Tells literal placement of robot vs. person (-1 xDiff =
					// person to left 1 space, -1 yDiff = person up 1 space)
					int xDiff = playerXPos - bots[counter].getXCoord();
					int yDiff = playerYPos - bots[counter].getYCoord();
					// Tells which direction is closer (useful for telling which
					// direction robot needs to move)
					int xMeasure = Math.abs(xDiff);
					int yMeasure = Math.abs(yDiff);
					
					playField.set(bots[counter].getYCoord() * 20 + bots[counter].getXCoord(), ListAdapter.EMPTY);
					
					// If person is closer along x-axis

					// CURRENTLY: It's almost impossible to win.
					// DETECT COLLISION AFTER THE ROBOT MOVES, DETECT IF TWO
					// DIFFERENT ROBOTS HAVE THE SAME COORDINATES
					if (xMeasure > yMeasure) {
						// Person is to the left on x-axis
						if (xDiff < 0) {
							bots[counter]
									.setXCoord(bots[counter].getXCoord() - (1));
							// Person is to right on x-axis
						} else if (xDiff > 0) {
							bots[counter]
									.setXCoord(bots[counter].getXCoord() + (1));
						}
						// If person is further along y-axis
					} else if (yMeasure > xMeasure) {
						// Person is above on y-axis, robot moves up
						if (yDiff < 0) {
							bots[counter]
									.setYCoord(bots[counter].getYCoord() - (1));
							// Person is below robot on y-axis
						} else if (yDiff > 0) {
							bots[counter]
									.setYCoord(bots[counter].getYCoord() + (1));
						}
						// Person is equally far along x and y axis
					} else if (yMeasure == xMeasure) {

						if (xDiff != 0 && yDiff != 0) {
							// Person with same x and y can only be in 4
							// quadrants
							// Person up and to the right, robot moves
							// diagonally
							if (xDiff > 0 && yDiff < 0) {
								bots[counter].setYCoord(bots[counter]
										.getYCoord() - (1));
								bots[counter].setXCoord(bots[counter]
										.getXCoord() + (1));
								// Person is up and to the left, robot moves
								// diagonally
							} else if (xDiff < 0 && yDiff < 0) {
								bots[counter].setYCoord(bots[counter]
										.getYCoord() - (1));
								bots[counter].setXCoord(bots[counter]
										.getXCoord() - (1));
								// Person is down and to the left, robot moves
								// diagonally
							} else if (xDiff < 0 && yDiff > 0) {
								bots[counter].setYCoord(bots[counter]
										.getYCoord() + (1));
								bots[counter].setXCoord(bots[counter]
										.getXCoord() - (1));
								// Person is down and to the right, robot moves
								// diagonally
							} else if (xDiff > 0 && yDiff > 0) {
								bots[counter].setYCoord(bots[counter]
										.getYCoord() + (1));
								bots[counter].setXCoord(bots[counter]
										.getXCoord() + (1));
							}
						}
					}
					
					playField.set(bots[counter].getYCoord() * 20 + bots[counter].getXCoord(), ListAdapter.ROBOT);


				}
				collisionCheck(x, bots[counter], counter);
			}

		}
		field.invalidateViews();
		
		winCheck();
		return playField;
	}

	// PLAYER SECTION
	public ArrayList<Integer> placePlayer(Player y) {
		y.setIsDead(false);
		Random rand = new Random();
		y.setXCoord(rand.nextInt(20));
		y.setYCoord(rand.nextInt(20));
		
		playField.set(y.getYCoord() * 20 + y.getXCoord(), ListAdapter.PLAYER);
		field.invalidateViews();
		
		return playField;

	}

	public ArrayList<Integer> movePlayer(String x, Player player) {

		playField.set(player.getYCoord() * 20 + player.getXCoord(), ListAdapter.EMPTY);
		
		if (x.equalsIgnoreCase("d") || x.equalsIgnoreCase("down")) {
			if (player.getYCoord() + (1) < height) {
				player.setYCoord(player.getYCoord() + 1);
			}
		} else if (x.equalsIgnoreCase("u") || x.equalsIgnoreCase("up")) {
			if (player.getYCoord() > 0) {
				player.setYCoord(player.getYCoord() - 1);
			}
		} else if (x.equalsIgnoreCase("l") || x.equalsIgnoreCase("left")) {
			if (player.getXCoord() > 0) {
				player.setXCoord(player.getXCoord() - 1);
			}
		} else if (x.equalsIgnoreCase("r") || x.equalsIgnoreCase("right")) {
			if (player.getXCoord() + (1) < width) {
				player.setXCoord(player.getXCoord() + 1);
			}
		} else if (x.equalsIgnoreCase("t") || x.equalsIgnoreCase("teleport")) {
			if (teleportCounter != 0) {
				teleportCounter--;
				placePlayer(player);
			}
		}
		
		playField.set(player.getYCoord() * 20 + player.getXCoord(), ListAdapter.PLAYER);
		field.invalidateViews();
		
		return playField;
	}

	// LAYOUT PARAMMMSSSS
	// TEXT VIEW MULTIPLE LINES
//	public String drawPlayField() {
//		temp = new String();
//		for (int i = 0; i < 22; i++) {
//			for (int j = 0; j < 22; j++) {
//				temp += playField[i][j];
//			}
//			temp += "\n";
//		}
//		return temp;
//	}

	public void setField(ArrayList<Integer> field) {
		playField = field;
	}

	public ArrayList<Integer> getField() {
		return playField;
	}

	public void collisionCheck(Player player, Robot robot, int counter) {
		// compares each robot location to each other, as well as player.
		if (robot.getXCoord() == player.getXCoord()
				&& robot.getYCoord() == player.getYCoord()) {
			player.setIsDead(true);
		}
		for (int i = 0; i < robotCount; i++) {
			if (i != counter) {
				if (bots[i].getXCoord() == robot.getXCoord()
						&& bots[i].getYCoord() == robot.getYCoord()) {
					bots[i].setIsDead(true);
					robot.setIsDead(true);
					
					playField.set(bots[i].getYCoord() * 20 + robot.getXCoord(), ListAdapter.ROBOT);
				}
			}
		}
		
		field.invalidateViews();
	}

	public void winCheck() {
		int checker = 0;
		for (int i = 0; i < robotCount; i++) {
			if (bots[i].getIsDead() == true) {
				checker++;
			}
		}
		if (checker == robotCount) {
			winToken = true;
		}
	}

	public boolean getWinToken() {
		return winToken;
	}

	public String getTeleports() {
		return "" + teleportCounter;
	}

}